using System.Collections.Generic;
using Engine.Core.Interfaces;
using Engine.Particles;
using Engine.Particles.Effects;
using EngineDevelopment.DependencyInjection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ninject;

namespace EngineDevelopment
{
    public class EngineDevelopment : InjectedGame
    {
        readonly GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        public Dictionary<string, Texture2D> TexturesDict;
        private SpriteFont _outputFont;

        [Inject]
        public IRenderEngine RenderEngine { get; set; }

        [Inject]
        public ParticleEngine ParticleEngine { get; set; }

        public EngineDevelopment()
        {
            Kernel.Inject(this);
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            TexturesDict = new Dictionary<string, Texture2D>();
            base.Initialize();
            //_graphics.PreferredBackBufferWidth = 1920;
            //_graphics.PreferredBackBufferHeight = 1080;
            //_graphics.ToggleFullScreen();
            //_graphics.ApplyChanges();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            TexturesDict.Add("explosion", Content.Load<Texture2D>(@"textures\particles\circle"));
            Content.Load<Effect>(@"effects\particles\lightning");
            _outputFont = Content.Load<SpriteFont>(@"fonts\debug");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            var mouseLocation = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);

            var keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Home))
            {
                _graphics.ToggleFullScreen();
                _graphics.ApplyChanges();
            }
            if (keyboard.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                ParticleEngine.AddParticleEffect(ExplosionEffect.Create(TexturesDict["explosion"], mouseLocation,true));
            }

            ParticleEngine.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            RenderEngine.AddObjectsForRendering(ParticleEngine.GetDrawables());

            RenderEngine.AddAdditionalRenderActions(
                x => x.Draw(TexturesDict["explosion"], new Vector2(Mouse.GetState().X - TexturesDict["explosion"].Width/2, Mouse.GetState().Y - TexturesDict["explosion"].Height/2), Color.White),
                x => x.DrawString(_outputFont, Framerate, Vector2.Zero, Color.White));

            RenderEngine.Render(_spriteBatch, gameTime);
            
            base.Draw(gameTime);
        }
    }
}
